package com.china08.yxyapi.api.manage.stu.mystudy.homework.workreport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.stu.mystudy.homework.dowork.impl.HomeworkServiceImpl;

/**
 * 作业提交 2017年3月11日09:15:54
 * 
 * @author lys
 *
 */
@CrossOrigin
@RestController
public class MassageWorkReportFinishworkController extends YxyApiBaseController {
    @Autowired
    HomeworkServiceImpl homeworkSrevice;

    @Autowired
    private HomeworkTaskReportRepository htrr;

    /**
     * 通知类作业学生点击提交按钮后的操作 2017年3月11日09:27:11
     * 
     * @param reqModel
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/workreport/massagefinishwork", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        RespModel respModel = new RespModel();
        // 得到参数并验证
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.studentId, null), "作业的ID不能为空");
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.taskId, null), "的ID不能为空");
        // 验证是否有提交权限
        // 如果任务类型为2 且已经提交过作业操作那么禁止用户进行提交当前题
        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId, reqModel.taskId);
        Assert.isTrue(homeworkTaskReport.getStatusOfFinished() != 1, "当前任务下的试题作业已提交！");
        // 提交当前作业
        homeworkSrevice.commitHomework(reqModel.studentId, reqModel.taskId, "通知");
        respModel.setMessage("作业提交成功");
        return respModel;

    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 任务ID
        public String taskId;
        // 学生ID
        public String studentId;
        // 作业ID
        public String quesId;
        public Integer duration;
        // 答案:我的 ；可能为空，因为主观题为图片链接 <br/> 多选题，使用A，B 存储
        public String answerOfMy;
    }

}
